<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends MX_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->display->set_theme('cms');
	}
	
	public function index()
	{
		if( ! $this->ion_auth->is_admin() )
		{
			redirect(config_item('cms_uri').'/auth/login');
		}		
	}
	
	public function login()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('login', 'Login', 'required');
		$this->form_validation->set_rules('pass', 'Password', 'required');
		
		if( $this->input->is_ajax_request() && $this->input->post())
		{
			if($this->form_validation->run() == TRUE)
			{
				$remember = (bool) $this->input->post('remember');
				if ($this->ion_auth->login($this->input->post('login'), $this->input->post('pass'), $remember))
				{ 
					$this->display->array_json(array('logged' => TRUE));
				}
				else
				{
					$this->display->array_json(array('logged' => FALSE, 'errors' => $this->ion_auth->errors()));
				}
			}
			else
			{
				$this->display->array_json(array('logged' => FALSE, 'errors' => validation_errors()));
			}
			
		}
		else
		{
			$this->twiggy->title()->append('Login');
			$this->display->template('login', array('user' => 'yugo'));	
		}
	
	}
	
	public function logout()
	{
		$this->ion_auth->logout();
		cms_redirect('auth/login');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */